import 'dart:io';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPreview extends StatefulWidget {
  final String? url;

  const VideoPreview({
    Key? key,
    required this.url,
  }) : super(key: key);

  @override
  VideoPreviewState createState() => VideoPreviewState();
}

class VideoPreviewState extends State<VideoPreview> {
  late VideoPlayerController _controller;
  @override
  void initState() {
    super.initState();
    final url = widget.url;
    if (url == null) return;
    if (url.contains("http://") || url.contains("https://")) {
      // ignore: deprecated_member_use
      _controller = VideoPlayerController.network(url);
    } else {
      _controller = VideoPlayerController.file(File(url));
    }
  }

  Future<bool> started() async {
    await _controller.initialize();
    await _controller.play();
    _controller.setLooping(true);
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.black,
        body: GestureDetector(
          behavior: HitTestBehavior.opaque,
          child: Center(
            child: FutureBuilder<bool>(
              future: started(),
              builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
                if (snapshot.data == true) {
                  return AspectRatio(
                    aspectRatio: _controller.value.aspectRatio,
                    // child: VideoPlayer(_controller),
                    child: Stack(
                      alignment: Alignment.bottomCenter,
                      children: <Widget>[
                        VideoPlayer(_controller),
                        // 字幕
                        // ClosedCaption(
                        //     text: _controller.value.caption.text),
                        // buildTap(context, _controller), // 点击播放、暂停，显示暂停图标
                        VideoProgressIndicator(
                          _controller,
                          colors: const VideoProgressColors(
                              playedColor: Colors.white38,
                              bufferedColor: Colors.white12,
                              backgroundColor: Colors.white10),
                          allowScrubbing: true,
                        ), // 进度
                      ],
                    ),
                  );
                } else {
                  // return const Text('waiting for video to load');
                  return const SizedBox.shrink();
                }
              },
            ),
          ),
          onTap: () {
            Navigator.of(context).pop();
          },
        ));
  }

  @override
  void dispose() {
    _controller.pause();
    _controller.dispose();
    super.dispose();
  }

  // 点击播放、暂停，显示暂停图标
  Widget buildTap(BuildContext context, VideoPlayerController controller) {
    return Stack(
      children: <Widget>[
        AnimatedSwitcher(
          duration: const Duration(milliseconds: 50),
          reverseDuration: const Duration(milliseconds: 200),
          child: controller.value.isPlaying
              ? const SizedBox.shrink()
              : Container(
                  color: Colors.black26,
                  child: const Center(
                    child: Icon(Icons.play_circle_outline,
                        color: Colors.white30, size: 65.0),
                  ),
                ),
        ),
        GestureDetector(
          onTap: () {
            // 点击播放/暂停
            controller.value.isPlaying ? controller.pause() : controller.play();
          },
        ),
      ],
    );
  }
}
